package com.itheima.health.controller.mobile;


import com.itheima.health.common.MessageConst;
import com.itheima.health.dto.OrderSubmitParam;
import com.itheima.health.pojo.Order;
import com.itheima.health.result.Result;
import com.itheima.health.service.OrderPersonService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/mobile/order/")
public class OrderPersonController {

    @Autowired
    private OrderPersonService orderPersonService;
    @Autowired
    private OrderSettingService orderSettingService;

    @PostMapping("/submit")
    public Result submit(@RequestBody OrderSubmitParam orderSubmitParam) {
        OrderSubmitParam person = orderPersonService.getOrderByIdCardAndOrderDate(orderSubmitParam.getIdCard(), orderSubmitParam.getOrderDate());
        //已经预约了,提示预约过了
        if (person != null) {
            return new Result(false, MessageConst.HAS_ORDERED);
        }
        //没预约过，判断是否还可以预约
        boolean status = orderSettingService.findByDate(orderSubmitParam.getOrderDate());
        if (status) {
            return new Result(false, MessageConst.ORDER_FULL);
        }

        //没有预约，就插入
        orderPersonService.add(orderSubmitParam);

        Map<String, Integer> orderId = new HashMap<>();
        orderId.put("id", orderSubmitParam.getId());
        return new Result(true, MessageConst.ORDER_SUCCESS, orderId);
    }

    @GetMapping("/findById")
    public Result findById(Integer id) {
        OrderVO order = orderPersonService.findById(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, order);
    }


}
